﻿/* Write methods that calculate the surface of a triangle by given:
Side and an altitude to it; Three sides; Two sides and an angle between them. Use System.Math.
 */
using System;
using System.Globalization;
using System.Threading;

namespace _4.CalculateSurfaceOfaTriangle
{
    class CalculateSurfaceOfaTriangle
    {
        public static int choice;
        static void Main()
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
            do
            {
                PrintMenu();
            } while (choice != 0);
        }

        private static void PrintMenu()
        {
            do {
                Console.WriteLine("Please choose an action: ");
                Console.WriteLine(@"
    1. Calculate triangle surface by entering a side and an altitude
    2. Calculate triangle surface by entering three sides
    3. Calculate triangle surface by entering two sides and an angle
    0. Exit
                ");
                bool iProvided = false;
                do
                {
                    if (iProvided == true)
                    {
                        Console.WriteLine("Error: Invalid input! Please enter a valid number from the menu.");
                        PrintMenu();
                    }
                    else iProvided = true;
                    Console.Write("Please enter your preference: ");
                } while (Int32.TryParse(Console.ReadLine(), out choice) == false);
                switch (choice)
                {
                    case 1: 
                        Calcbysidealt_getparams();
                        break;
                    case 2:
                        Calcbysides_getparams();
                        break;
                    case 3:
                        Calcbysideswithangle_getparams();
                        break;
                }
            } while (choice < 0 || choice > 3);

        }

        private static void Calcbysideswithangle_getparams()
        {
            Console.Write("Enter first side: ");
            double first_side_param = double.Parse(Console.ReadLine());
            Console.Write("Enter second side: ");
            double second_side_param = double.Parse(Console.ReadLine());
            Console.Write("Enter the angle between the above sides: ");
            double angle_param = double.Parse(Console.ReadLine());
            Console.WriteLine("Calculated surface: {0:0.000}", Calcbysideswithangle(first_side_param, second_side_param, angle_param));

        }

        private static void Calcbysides_getparams()
        {
            Console.Write("Enter first side: ");
            double first_side_param = double.Parse(Console.ReadLine());
            Console.Write("Enter second side: ");
            double second_side_param = double.Parse(Console.ReadLine());
            Console.Write("Enter third side: ");
            double third_side_param = double.Parse(Console.ReadLine());
            Console.WriteLine("Calculated surface: {0:0.000}", Calcbysides(first_side_param, second_side_param, third_side_param));
        }

        private static void Calcbysidealt_getparams()
        {
            Console.Write("Enter side: ");
            double side_param = double.Parse(Console.ReadLine());
            Console.Write("Enter altitude: ");
            double altitude_param = double.Parse(Console.ReadLine());
            Console.WriteLine("Calculated surface: {0}", Calcbysidealt(side_param, altitude_param));
        }
        /* calculate by side and altitude */
        public static double Calcbysidealt(double side, double altitude)
        {
            return side * altitude / 2;
        }        
        /* calculate by three sides */
        public static double Calcbysides(double a, double b, double c)
        {
            double sp = (a + b + c) / 2; // semi-peremeter
            return Math.Sqrt(sp * (sp - a) * (sp - b) * (sp - c));
        }
        /* calculate by Two sides and an angle between them */
        public static double Calcbysideswithangle(double side1, double side2, double angle)
        {
            return side1 * side2 * Math.Sin(Math.PI * angle / 180) / 2;
        }


    }
}
